package com.cjm.one.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.model.base.PasswordDto;
import com.cjm.one.model.system.dto.SysUserInfoDto;
import com.cjm.one.model.system.dto.SysUserInfoFromDto;
import com.cjm.one.model.system.entity.SysRole;
import com.cjm.one.model.system.entity.SysUserPost;
import com.cjm.one.model.system.entity.SysUserRole;
import com.cjm.one.model.system.vo.SysUserInfoVo;
import com.cjm.one.system.mapper.*;
import com.cjm.one.system.service.SysUserInfoService;
import com.cjm.one.model.system.entity.SysUserInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 29289
* @description 针对表【sys_user_info(用户表)】的数据库操作Service实现
* @createDate 2024-09-05 16:21:14
*/
@Service
@Slf4j
public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUserInfo>
    implements SysUserInfoService {
    @Resource
    private SysUserInfoMapper sysUserInfoMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysPostMapper sysPostMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysUserPostMapper sysUserPostMapper;

    @Override
    public SysUserInfo getByUserName(String username) {

// TODO 分布式事务完善
        SysUserInfo sysUserInfo = null;


             sysUserInfo = sysUserInfoMapper.selectByUsername(username);
            List<SysRole> roleList =  sysUserRoleMapper.selectByUserId(sysUserInfo.getId());
            sysUserInfo.setRoleList(roleList);


            log.info("sysUserInfo:{}",sysUserInfo);
            return sysUserInfo;


    }

    @Override
    public boolean saveUser(SysUserInfo sysUserInfo) {
        int rows = sysUserInfoMapper.insert(sysUserInfo);
        return rows == 1;
    }

    @Override
    public SysUserInfo getSysUserById(Long id) {
        SysUserInfo sysUserInfo = sysUserInfoMapper.selectById(id);
        return sysUserInfo;
    }

    //根据id修改系统用户信息
    @Override
    public boolean updatSysUsereById(SysUserInfoDto sysUserInfoDto) {
//        // TODO 分布式事务完善
        //根据系统用户id删除岗位信息和角色信息
        if (sysUserInfoDto.getPostIds()!=null){
            sysPostMapper.deleteBySysUserId(sysUserInfoDto.getId());
        }
        if (sysUserInfoDto.getRoleIds()!=null){
            sysRoleMapper.deleteBySysUserId(sysUserInfoDto.getId());
        }

        //插入新的岗位和角色信息
        if (sysUserInfoDto.getPostIds()!=null&&sysUserInfoDto.getPostIds().length()>0){
            List<Long> postIds = Arrays.stream(sysUserInfoDto.getPostIds().split(","))
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            sysPostMapper.insertSysUserPost(sysUserInfoDto.getId(),postIds);
        }
        if (sysUserInfoDto.getRoleIds()!=null&&sysUserInfoDto.getRoleIds().length()>0){
            List<Long> roleIds = Arrays.stream(sysUserInfoDto.getRoleIds().split(","))
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            sysRoleMapper.insertSysUserRoles(sysUserInfoDto.getId(),roleIds);
        }

        //更新系统用户信息
        SysUserInfo sysUserInfo = new SysUserInfo();
        BeanUtils.copyProperties(sysUserInfoDto, sysUserInfo);
        int rows = sysUserInfoMapper.updateSysUserInfoById(sysUserInfo);
        //
        return rows==1;
    }

    @Override
    public boolean deleteSysUser(Long id) {
        // TODO 分布式事务完善
        //删除对应的用户岗位关系
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserPost::getUserId, id);
        sysUserPostMapper.delete(queryWrapper);
        //删除对应的用户角色关系
        LambdaQueryWrapper<SysUserRole> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SysUserRole::getUserId, id);
        sysUserRoleMapper.delete(queryWrapper1);

        int rows = sysUserInfoMapper.deleteById(id);
        return rows==1;
    }
    //修改密码
    @Override
    public boolean updateSysUserePasswordById(PasswordDto passwordDto) {

        String password = passwordDto.getPassword();
        PasswordEncoder customPasswordEncoder = new BCryptPasswordEncoder();
        String encodePassword = customPasswordEncoder.encode(password);
        passwordDto.setPassword(encodePassword);
        int rows = sysUserInfoMapper.updateSysUserePasswordById(passwordDto);
        return rows == 1;
    }

    @Override
    public boolean updateSysUsereStatusById(SysUserInfoDto sysUserInfoDto) {
        int rows = sysUserInfoMapper.updateSysUsereStatusById(sysUserInfoDto);
        return rows == 1;
    }

    @Override
    public List<SysUserRole> getSysUserRoleList(Long userId) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        return sysUserRoleMapper.selectList(queryWrapper);
    }

    @Override
    public Page<SysUserInfoVo> getSysUserPage(Page<SysUserInfoVo> page, SysUserInfoFromDto queryVo) {
        return  sysUserInfoMapper.selectPageSysUser(page,queryVo);
    }

    @Override
    public Integer deleteSysUsereMore(List<Long> ids) {
        // TODO 分布式事务完善
        //删除对应的用户岗位关系
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUserPost::getUserId, ids);
        sysUserPostMapper.delete(queryWrapper);
        //删除对应的用户角色关系
        LambdaQueryWrapper<SysUserRole> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SysUserRole::getUserId, ids);
        sysUserRoleMapper.delete(queryWrapper1);

        int rows = sysUserInfoMapper.deleteBatchIds(ids);
        log.info("删除用户信息成功rows:{}",rows);
        return rows;
    }

    @Override
    public Boolean resetPassword(PasswordDto passwordDto) {
        LambdaUpdateWrapper<SysUserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUserInfo::getId,passwordDto.getId());
        PasswordEncoder customPasswordEncoder = new BCryptPasswordEncoder();
        String encodePassword = customPasswordEncoder.encode("123456");
        updateWrapper.set(SysUserInfo::getPassword,encodePassword);
        int rows = sysUserInfoMapper.update(null, updateWrapper);
        return rows ==1;
    }
}




